/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.octopeer.core.config;

import java.io.File;

import java.util.ArrayList;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;


import net.octopeer.core.Peer;
import net.octopeer.core.PeerNode;
import net.octopeer.core.Neighbor;
import net.octopeer.utilities.XMLTools;

/**
 *
 * @author andria
 */
public class NeighborListConfig {
    
    public NeighborListConfig() {
        
    }
    
    public ArrayList<Neighbor> readNeighborListConfig() throws Exception {
        ArrayList<Neighbor> neighborList = new ArrayList<Neighbor>();
        
        Document dom = null;
        File file = null;
        Peer result = null;
        XMLTools xt = new XMLTools();
        
        // Parse the XML stream input to Document type
        file = new File("./conf/neighborList.xml");
        dom = xt.db.parse(file);
        
        Element rootEle = dom.getDocumentElement();
        
        NodeList peerNeighborListList = rootEle.getElementsByTagName("NeighborList");
        NodeList peerNeighborList = rootEle.getElementsByTagName("NeighborList:Neighbor");

        int peerNeighborLoop = 0;
        while (peerNeighborLoop < peerNeighborList.getLength()) {
            NodeList peerNeighborNodes = peerNeighborList.item(peerNeighborLoop).getChildNodes();

            Neighbor peerNeighbor = new Neighbor();
            PeerNode peerNeighborNode = new PeerNode();

            NodeList peerNeighborPeerNode = peerNeighborNodes.item(0).getChildNodes();

            String peerNeighborNodeIDText = peerNeighborPeerNode.item(0).getFirstChild().getNodeValue();
            String peerNeighborNodeIPaddressText = peerNeighborPeerNode.item(1).getFirstChild().getNodeValue();
            String peerNeighborNodePortText = peerNeighborPeerNode.item(2).getFirstChild().getNodeValue();

            peerNeighborNode.setNodeID(peerNeighborNodeIDText);
            peerNeighborNode.setNodeIPaddress(peerNeighborNodeIPaddressText);
            peerNeighborNode.setNodePort(Integer.valueOf(peerNeighborNodePortText));

            NodeList peerNeighborRankList = rootEle.getElementsByTagName("NeighborList:Neighbor:Rank");
            String peerNeighborRankText = peerNeighborNodes.item(1).getFirstChild().getNodeValue();

            peerNeighbor.setNode(peerNeighborNode);
            peerNeighbor.setRank(Integer.valueOf(peerNeighborRankText));

            neighborList.add(peerNeighbor);

            peerNeighborLoop++;
        }
        
        return neighborList;
    }
    
    public void updateNeighborListConfig(Peer peer) throws Exception {
        ArrayList<Neighbor> neighborList = peer.getNeighborList();
        
        // Prepare the DOM document
        Document doc;
        XMLTools xt = new XMLTools();        
        doc = xt.db.newDocument();
        
        Element rootEle = doc.createElement("NeighborList");
        
        int neighborLoop = 0;
        while (neighborLoop < neighborList.size()) {
            Element neighborEle = doc.createElement("NeighborList:Neighbor");
            
            Neighbor neighbor = (Neighbor) neighborList.get(neighborLoop);
            PeerNode neighborNode = neighbor.getNode();
            
            // Node
            Element neighborNodeEle = doc.createElement("NeighborList:Neighbor:Node");

            // ID
            Element neighborNodeIDEle = doc.createElement("NeighborList:Neighbor:Node:ID");
            Text neighborNodeIDText = doc.createTextNode(neighborNode.getNodeID());
            neighborNodeIDEle.appendChild(neighborNodeIDText);
            neighborNodeEle.appendChild(neighborNodeIDEle);        

            // IPaddress
            Element neighborNodeIPaddressEle = doc.createElement("NeighborList:Neighbor:Node:IPaddress");
            Text neighborNodeIPaddressText = doc.createTextNode(neighborNode.getNodeIPaddress());
            neighborNodeIPaddressEle.appendChild(neighborNodeIPaddressText);
            neighborNodeEle.appendChild(neighborNodeIPaddressEle);
            
            // Port
            Element neighborNodePortEle = doc.createElement("NeighborList:Neighbor:Node:Port");
            Text neighborNodePortText = doc.createTextNode(Integer.toString(neighborNode.getNodePort()));
            neighborNodePortEle.appendChild(neighborNodePortText);
            neighborNodeEle.appendChild(neighborNodePortEle);

            neighborEle.appendChild(neighborNodeEle);

            // Rank
            Element neighborRankEle = doc.createElement("NeighborList:Neighbor:Rank");
            Text neighborRankText = doc.createTextNode(Integer.toString(neighbor.getRank()));
            neighborRankEle.appendChild(neighborRankText);
            neighborEle.appendChild(neighborRankEle);    
            
            rootEle.appendChild(neighborEle);
            
            neighborLoop++;
        }    
        
        doc.appendChild(rootEle);      
        
        // Prepare the DOM document for writing
        Source source = new DOMSource(doc);

        // Prepare the output file
        File file = new File("./conf/neighborList.xml");
        Result result = new StreamResult(file);

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);        
    }
}
